package org.mzyyn.qr.util;


import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lipeng
 * @version v1.0.0
 * @date 2021-11-2
 * @description
 */
@Slf4j
public class HttpUtil {

    public static void writeJson(HttpServletResponse resp, JSONObject json) {
        PrintWriter out = null;
        try {
            resp.setContentType("application/json;charset=UTF-8");
            out = resp.getWriter();
            out.write(json.toJSONString());
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
//    /**
//     * 向指定URL发送GET方法的请求
//     *
//     * @param url 发送请求的URL
//     * @return URL 所代表远程资源的响应结果
//     */
//    public static String sendGet(String url) {
//
//        Request.Builder builder = new Request.Builder().method("GET",null)
//                .url(url);
//        builder.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
//        builder.addHeader("accept", "*/*");
//        builder.addHeader("connection", "Keep-Alive");
//        OkHttpClient client = new OkHttpClient();
//        Response response = null;
//        try {
//            response = client.newCall(builder.build()).execute();
//            return response.body().string();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    /**
//     * 向指定URL发送GET方法的请求
//     *
//     * @param url 发送请求的URL
//     * @param header HEADER头
//     * @return 所代表远程资源的响应结果
//     */
//    public static String sendGet(String url, Map<String,String> header) {
//        Request.Builder builder = new Request.Builder().method("GET",null)
//                .url(url);
//        builder.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
//        for(Map.Entry<String, String> entry : header.entrySet()){
//            String mapKey = entry.getKey();
//            String mapValue = entry.getValue();
//            builder.addHeader(mapKey,mapValue);
//        }
//        OkHttpClient client = new OkHttpClient();
//        Response response = null;
//        try {
//            response = client.newCall(builder.build()).execute();
//            return response.body().string();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//
//    }
//
//
//    /**
//     * 获取COOKIE列表
//     *
//     * @param address
//     * @return
//     * @throws IOException
//     */
//    public static Map<String, String> getCookies(String address) throws IOException {
//        Map<String, String> outMap = new HashMap<>();
//        HashMap<HttpUrl,List<Cookie>> cookieStore=new HashMap<>();
//
//
//        Request.Builder builder = new Request.Builder().method("GET",null)
//                .url(address);
//        OkHttpClient client = new OkHttpClient();
//        Response response = client.newCall(builder.build()).execute();
//        Headers headers = response.headers();
//        List<String> cookies = headers.values("Set-Cookie");
//
//        cookies.forEach(v->{
//            String value = StringUtils.substringBetween(v, "=", ";");
//            String key = StringUtils.substringBefore(v,"=");
//            outMap.put(key,value);
//        });
//
//        return outMap;
//
//    }

}
